#!/usr/bin/env python

import sys
import time
import dns.message
import os
import subprocess
from socket import socket, AF_INET, SOCK_DGRAM


#honey_ip = "202.108.8.81"
bind_ip = "0.0.0.0"
bind_port = 54321 
#interface="venet0:0"

def usage():
    print "%s <name_file> <fake_dns_ip>" % sys.argv[0]

def send_packet(ip, udp, query):
    if not ip or not udp or not query:
        return False

    try:
        udp.sendto(query, (ip, 53))
    except Exception as e:
        print "send to %s error!\n" % ip

def sender(honey_ip, domain_f):

    try:
	f=open(domain_f)
    except:
	return
	
    i = 0
    udp = socket(AF_INET, SOCK_DGRAM)
    udp.bind((bind_ip, bind_port))

    #for domain in domains:
    for domain in f:
        query = dns.message.make_query(domain.strip(), 1).to_wire()
        send_packet(honey_ip, udp, query)
        i += 1
        if not i % 100:
	    time.sleep(0.5)

    f.close()

def main(): #(domain_file, dump_file)

    domain_file=sys.argv[1]
    honey_ip=sys.argv[2]

    sender(honey_ip,domain_file)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        usage()
        exit()

    main()

